package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.GsonManager;
import com.sgb.kjwl.databinding.ItemSearchServiceMapBinding;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;

import java.util.ArrayList;
import java.util.List;

public class SearchServiceMapAdapter extends BaseRecycleAdapter<EpQuoteEntity> {

    public SearchServiceMapAdapter(Context context, List<EpQuoteEntity> datas) {
        super(context, R.layout.item_search_service_map, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final EpQuoteEntity epQuoteEntity) {
        holder.setBinding(BR.viewModel, epQuoteEntity);
        ItemSearchServiceMapBinding mapBinding = (ItemSearchServiceMapBinding) holder.getmBinding();
        mapBinding.tvShowMoreService.setOnClickListener(view -> {
            if (iShowMore != null) {
                iShowMore.showMore(epQuoteEntity.getCompanyNo());
            }
        });

        String cities = epQuoteEntity.getServiceCitys();
        ArrayList<BaseAreaEntity> citysList = GsonManager.getInstance().getGson().fromJson(cities, new TypeToken<ArrayList<BaseAreaEntity>>() {
        }.getType());
        StringBuilder stringBuilder = new StringBuilder();
        for (BaseAreaEntity entity : citysList) {
            stringBuilder.append(entity.getName()).append("/");
        }
        String cityShow = stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);
        mapBinding.tvSearchServiceMapCity.setText("服务城市：" + (TextUtils.isEmpty(cityShow) ? "/" : cityShow));

        String subStr = "/";
        if (epQuoteEntity.getFirstCategoryName() != null && !epQuoteEntity.getFirstCategoryName().equals("")) {
            subStr = epQuoteEntity.getFirstCategoryName();
            if (epQuoteEntity.getSecondCategoryName() != null && !epQuoteEntity.getSecondCategoryName().equals("")) {
                String two = epQuoteEntity.getSecondCategoryName();
                subStr = subStr + "-" + two;
            }
            if (epQuoteEntity.getItemName() != null && !epQuoteEntity.getItemName().equals("")) {
                String three = epQuoteEntity.getItemName();
                subStr = subStr + "-" + three;
            }
        }
        mapBinding.tvSearchServiceMapCategory.setText("服务分类：" + subStr);
    }

    private IShowMore iShowMore;

    public interface IShowMore {
        void showMore(String companyId);
    }

    public void OnShowMore(IShowMore iShowMore) {
        this.iShowMore = iShowMore;
    }

}
